package com.ooqiu.gaming.ui.main.thread.interact;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.base.BaseFragment;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.ui.web.WebActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = InteractContentFragment.PATH)
public class InteractContentFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    public static final String PATH = "/fragment/interact/content";

    @BindView(R.id.rv_interact_content)
    RecyclerView mRvInteractContent;


    InteractAdapter mInteractAdapter;
    List<InteractEntity> interacts = new ArrayList<>();

    GamingApi mGamingApi;

    int page;
    @Autowired
    int statue;
    @BindView(R.id.swiper)
    SwipeRefreshLayout mSwiper;


    @Override
    public void initInjector(View view) {
        ButterKnife.bind(this, view);
        mGamingApi = getApplicationComponent().getGamingApi();

    }

    @Override
    public int initContentView() {
        return R.layout.fragment_interact_content;
    }

    @Override
    public void initUI(View view) {

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mRvInteractContent.setLayoutManager(new LinearLayoutManager(mContext));
        mRvInteractContent.setAdapter(mInteractAdapter = new InteractAdapter(interacts));

        mInteractAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InteractEntity entity = (InteractEntity) adapter.getItem(position);
                getRouter().build(WebActivity.PATH)
                        .withString("url", entity.getUrl())
                        .navigation();


            }
        });


        View empty = LayoutInflater.from(mContext).inflate(R.layout.common_list_empty_view, null);

        mInteractAdapter.setEmptyView(empty);



        mInteractAdapter.setEnableLoadMore(true);
        mInteractAdapter.setOnLoadMoreListener(this, mRvInteractContent);
        mSwiper.setOnRefreshListener(this);
        onRefresh();

    }

    @Override
    public void initData() {

    }


    private void fetchIntractList(final boolean isRefresh) {
        mGamingApi.fetchInteractList(page, statue)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<InteractEntity>>(mContext) {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(List<InteractEntity> interactEntities) {
                        if (isRefresh) {
                            interacts.clear();
                        }

                        if (interactEntities.isEmpty()) {
                            mInteractAdapter.loadMoreEnd(page == 2);
                        } else {
                            mInteractAdapter.loadMoreComplete();
                        }
                        page++;
                        interacts.addAll(interactEntities);
                        mInteractAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {

                        mSwiper.setRefreshing(false);
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 1;
        mSwiper.setRefreshing(true);
        fetchIntractList(true);
    }



    @Override
    public void onLoadMoreRequested() {
        fetchIntractList(false);
    }
}
